import {defineStore} from 'pinia'
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
import {useMenuSessionStore} from "@/store/MenuSessionStore.js";

// 定义用户状态管理store
export const useUserStore = defineStore('user', {
    // 持久化配置
    persist: {
        key: 'user-store',
        storage: localStorage,
        paths: ['userInfo', 'token'] // 只持久化userInfo和token字段
    },

    // 状态定义
    state: () => ({
        userInfo: null, // 用户信息对象
        isLogin: false, // 登录状态
    }),

    // 计算属性
    getters: {},

    // 方法定义
    actions: {
        setLoginUserInfo(userInfo) {
            ElMessage.success("登录成功")
            this.userInfo = userInfo
            this.isLogin = true
        },
        setUserLogout() {
            const menuSessionStore = useMenuSessionStore()
            // menuSessionStore.activeRouteKey = "0-0"
            this.userInfo = null
            this.isLogin = false
            router.replace('/')
            ElMessage.success("退出成功")
        }
    }
})